/* 
* @Author: fangshuqin
* @Date:   2018-04-27 10:21:20
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-28 11:22:03
*/

{
    let EVENT_CLICK = Laya.Event.CLICK;
    class Battle extends ui.Hall.BattleUI {
        constructor() {
            super();

            this.init();
        }

        destroy() {
            super.destroy();
            Sail.io.unregister(this.ACTIONS);
        }

        init() {

            this.initConfig();
            this.initEvent();
            this.registerActions();
            Sail.io.emit(GAME_CMDS.GET_MAP_LIST);

            this.timeArr = [];
            this.currentTime = 0;

            // 创建倒计时器
            this.timeCountDown = Sail.Utils.createTimeCountDown();
        }

        // 初始化配置
        initConfig() {
            this.config = {
                MAX_WIDTH: 275,
                MIN_WIDTH: 21,

            }
            this.battleDate = null;
            this.battleIndex = 0;
        }


        registerActions() {
            this.ACTIONS = {
                [GAME_CMDS.GET_MAP_LIST]: this.getMapListType,
                [GAME_CMDS.GET_MAP_INFO]: this.getMapListType,
                // [GAME_CMDS.CHOOSEBATTLEMODE]: this.modeData
            };
            Sail.io.register(this.ACTIONS, this);
        }

        initEvent() {
            this.battleBox.selectHandler = new Laya.Handler(this, (index) => {
                CUR_SELECTED_MODE_BG = index;
                if (!Sail.Utils.checkLogin() || this.battleDate===null) { return; }

                let mapInfo = this.battleDate[index];
                let localMapInfo = Sail.store.getMapInfo(index+1);
                let desc = localMapInfo?  localMapInfo.des :  '暂未开启';
                if (mapInfo.status == 0 || mapInfo.status == 2) {
                    Sail.director.popScene(new Alert.Tips(desc));

                } else if (mapInfo.status == 1) {
                    this.modeData(mapInfo);
                    // Sail.io.emit(GAME_CMDS.CHOOSEBATTLEMODE, { mapId: mapInfo.mapid });
                }
            });
        }

        modeData(mapInfo) {
            Public.BetAndSet.I.exit();

            Laya.loader.load(ASSETS.MATCH.concat(ASSETS.ROLE), Laya.Handler.create(null, function (_data) {
                Sail.director.runScene(new Scene.Match("hall", _data));
            }, [mapInfo]));
        }

        getMapListType(data, code , msg) {
            if( code !==0 ) return;
            console.log("getMapListType",data);
            let battleDate = this.battleDate = data;
            battleDate.forEach((info, index) => {

                this.timeArr.push(info.time);

                let _battleitem = this.battleBox.getChildByName(`item${index}`);
                let _dom_title = _battleitem.getChildByName("title");
                let _dom_modeicon = _battleitem.getChildByName("modeicon");
                let _dom_mode = _battleitem.getChildByName("mode");

                let box_battleDesc = _battleitem.getChildByName("battleDesc");
                let box_first  = _battleitem.getChildByName("firstBox");
                let _dom_pattern = _battleitem.getChildByName("pattern");
                let _box_shadow = _battleitem.getChildByName("shadow");
                let _dom_time = box_battleDesc.getChildByName("time");
                let _dom_produce = box_battleDesc.getChildByName("produce");
                let _dom_progress = box_battleDesc.getChildByName("progress");

                let _dom_isfirst = box_first.getChildByName("isfirst");
                let _dom_desc = _box_shadow.getChildByName("desc");
                let _dom_lock = _box_shadow.getChildByName("lock");

                 // 首胜是否显示
                if( info.isShow == 1  && info.status !== 2) {
                    box_first.visible = true;
                }else{
                    box_first.visible = false;
                }

                if(info.status !== 2) {
                    let localMapInfo =  Sail.store.getMapInfo(index+1);
                    let time = Sail.Utils.toDetailTime(info.time);

                    _dom_mode.index = Number(localMapInfo.mode) -1;
                    _dom_modeicon.index = Number(localMapInfo.mode)  -1;
                    _dom_title.text = localMapInfo.name;
                    _dom_time.text = time;
                    _dom_isfirst.text = info.firstWin;
                    _dom_produce.text = info.produce + '/' + info.total;
                    _dom_pattern.skin = `res/hall/pattern_${info.map_id}.jpg`;

                     let percent = info.produce / info.total
                    let per = Math.floor(percent * 100);
                    let w = per / 100 * this.config.MAX_WIDTH;
                    w = w < this.config.MIN_WIDTH ? this.config.MIN_WIDTH : w;
                    _dom_progress.scrollRect = {x : 0, y : 0, width : w, height : 28};
                    _dom_progress.visible = true;
                }

                if (info.status == 0) {

                    // 待开启
                    _dom_desc.text = "获得四个英雄后解锁";
                    _box_shadow.visible = true;
                    _dom_lock.y = 123;
                    _dom_desc.y = 200;
                   
                } else if (info.status == 2) {

                    // 暂未开启
                     _dom_desc.text = "尽请期待";
                    box_battleDesc.visible = false;
                    _dom_mode.index = 5;
                    _dom_mode.x = 163;
                    _dom_modeicon.index = -1;
                    _dom_title.text = '';
                    _dom_pattern.skin = 'res/hall/pattern_5.jpg';
                    _box_shadow.visible = true;

                }
                
            })
            this.openTimeCountDown();
        }

        openTimeCountDown() {
            console.log("this.timeArr",this.timeArr[0]);

            let _battleitem = this.battleBox.getChildByName('item0');
            let box_battleDesc = _battleitem.getChildByName("battleDesc");
            let _dom_time = box_battleDesc.getChildByName("time");  

            let _dom_pattern = _battleitem.getChildByName("pattern");
            let _dom_mode = _battleitem.getChildByName("mode");
            let _dom_modeicon = _battleitem.getChildByName("modeicon");

             // 预告倒计时
        /*    this.timeCountDown.start(62, (time) => {
                let txtTime = Sail.Utils.toDetailTime(time);
                _dom_time.text = txtTime;
                if( txtTime ==0){
                     _dom_pattern.skin = 'res/hall/pattern_refresh.jpg';
                     _dom_mode.index = 6;
                     _dom_modeicon.visible = false;
                }
            })*/
         
        }


        exit() {
            Sail.io.unregister(this.ACTIONS);
        }

        resize(width, height, rate) {
            if (rate < 1) {
                this.scale(rate, rate);
            } else {
                this.scale(1, 1);
            }
        }

    }

    Sail.class(Battle, "Com.Hall.Battle");
}